package com.edroid.autorunner;

import android.content.res.Configuration;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.util.DisplayMetrics;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.FrameLayout.LayoutParams;

import com.edroid.apkhooker.R;
import com.edroid.apkhooker.home.BaseActivity;


public abstract class BaseDrawerActivity extends BaseActivity {
	private DrawerLayout mDrawerLayout;
    private ActionBarDrawerToggle mDrawerToggle;
    private FrameLayout startLayout;
    private CharSequence mDrawerTitle;

    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    	
    	setContentView(R.layout.activity_drawer_base);
    	
		mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
		mDrawerLayout.setDrawerListener(new MyDrawerListener());
//		mDrawerLayout.setDrawerTitle(GravityCompat.START, getString(R.string.app_name));
		
		startLayout = (FrameLayout) findViewById(R.id.start_drawer);
		View drawerView = getStartDrawer(startLayout);
		if(drawerView != null)
			startLayout.addView(drawerView, new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
		
		FrameLayout layout2 = (FrameLayout) findViewById(R.id.content);
		View contentView = getContent(layout2);
		if(contentView != null)
			layout2.addView(contentView, new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
		
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		getSupportActionBar().setDisplayShowHomeEnabled(false);
		mDrawerTitle = getTitle();

		mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.app_name, R.string.app_name);
    }
    
    protected String getTitle2() {
		return getSupportActionBar().getTitle().toString();
	}
    
    protected void setTitle2(CharSequence title) {
    	setTitle(title);
    	getSupportActionBar().setTitle(title);
    }
    
    protected void setSubTitle2(String title) {
    	getSupportActionBar().setSubtitle(title);
    }
    
    protected void setIcon2(Drawable icon) {
    	getSupportActionBar().setIcon(icon);
    }
    
    protected void setDrawerTitle(CharSequence title) {
    	mDrawerTitle = title;
    }
    
    /**
     * 设置左抽屉宽度（屏幕宽度占比 0~1.0）
     */
    public void setStartDrawerWidth(float percent) {
    	android.view.ViewGroup.LayoutParams p = startLayout.getLayoutParams();
		DisplayMetrics m = getResources().getDisplayMetrics();
		p.width = Math.round(m.widthPixels*percent);
	}
    
    public void closeDrawer() {
    	mDrawerLayout.closeDrawer(startLayout);
	}
    
    /**
     * @return 如果直接添加到了 root 则返回 null
     */
    protected abstract View getContent(ViewGroup root);

    /**
     * @return 如果直接添加到了 root 则返回 null
     */
    protected abstract View getStartDrawer(ViewGroup root);
	
	@Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);

        mDrawerToggle.syncState();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
	
	@Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        mDrawerToggle.onConfigurationChanged(newConfig);
    }
	
	public void onPanelOpened() {
    }
	
	private class MyDrawerListener implements DrawerLayout.DrawerListener {
        
		@Override
        public void onDrawerOpened(View drawerView) {
            mDrawerToggle.onDrawerOpened(drawerView);
            getSupportActionBar().setTitle(mDrawerTitle);
            
            onPanelOpened();
        }

        @Override
        public void onDrawerClosed(View drawerView) {
            mDrawerToggle.onDrawerClosed(drawerView);
            getSupportActionBar().setTitle(getTitle());
        }

        @Override
        public void onDrawerSlide(View drawerView, float slideOffset) {
            mDrawerToggle.onDrawerSlide(drawerView, slideOffset);
        }

        @Override
        public void onDrawerStateChanged(int newState) {
            mDrawerToggle.onDrawerStateChanged(newState);
        }
    }
}
